#include"SendFrameClient.h"

SendFrameClient::SendFrameClient(int flag):HClientBase(flag)
{
    datacollect = new DataCollect(10,1920,1080,30);
    dataEncoding = new DataEncoding(this,AV_CODEC_ID_H264,1920,1080,30,AV_PIX_FMT_YUV420P,datacollect);
}
SendFrameClient::~SendFrameClient(){
    delete datacollect;
    delete dataEncoding;
}

int SendFrameClient::startClient()
{
    sockaddr_in svrAdd;
    string data = "Hello!";
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    //1. 握手
    sendData(data,sock->s,serverAdd4);
    recvData(data,sock->s,svrAdd);
    cout<<"rcvFrameSer:"<<data<<endl;
    if(data != "OK"){
        //TODO:  
        cout<<"握手异常！"<<endl;
        return -1;
    }

    //2.开始采集并发送
     //开启一个线程采集数据
    thread t1(&DataCollect::run,datacollect);
    //开启一个线程编码
    thread t2(&DataEncoding::run,dataEncoding);
    t1.join();
    t2.join();
    sock->Hclose();
    cout<<"SendFrameClient close!"<<endl;
    return 0;
}

int SendFrameClient::send_a_frame_to_server(void *arg)
{
    auto start_time = std::chrono::high_resolution_clock::now();
    string* data = (string*)arg;
    int ret = 0;
    //分包发送,一个包1024字节
    //1 计算需要几个包
    int packnum = ((data->size() - 1) / Packet_Size) + 1;
    //2 分包发送
    SendFrameCliMsg::FrameMsg frameMsg;
    string aData;
    {
        frameMsg.set_flag(0);
        frameMsg.set_id(0);
    }
    if(packnum == 1){
        frameMsg.set_flag(3);
        frameMsg.set_framedata(*data);
        frameMsg.SerializeToString(&aData);
        ret += sendData(aData,sock->s,serverAdd4);
        cout<<"send a frame Packet:"<<data->size()<<"bit"<<endl;
        delete data;
        auto end_time = std::chrono::high_resolution_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
        std::cout << "send time: " << static_cast<double>(duration.count()) / 1e6<< " s" << std::endl;
        return ret;
    }
    frameMsg.set_framedata(data->substr(0,Packet_Size));
    frameMsg.SerializeToString(&aData);
    ret += sendData(aData,sock->s,serverAdd4);
    for(int i = 1;i<packnum-1;i++){
        {
            frameMsg.set_flag(1);
            frameMsg.set_id(i);
            frameMsg.set_framedata(data->substr(Packet_Size*i,Packet_Size));
        }
        frameMsg.SerializeToString(&aData);
        ret += sendData(aData,sock->s,serverAdd4);
    }
    {
        frameMsg.set_flag(2);
        frameMsg.set_id(packnum-1);
        frameMsg.set_framedata(data->substr(Packet_Size*(packnum-1),data->size() - Packet_Size*(packnum-1)));
    }
    frameMsg.SerializeToString(&aData);
    ret += sendData(aData,sock->s,serverAdd4);
    cout<<"send a frame Packet:"<<data->size()<<"bit"<<endl;
    delete data;
    auto end_time = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
    std::cout << "send time: " << static_cast<double>(duration.count()) / 1e6<< " s" << std::endl;
    return 0;
}

int SendFrameClient::chageFrameRate(int frameRate)
{

}

int SendFrameClient::chageBitRate(double bitRate)
{

}

int SendFrameClient::chageResolutionRate(int w, int h)
{

}
